const app = getApp()
const WXAPI = require('../../wxapi/main')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        balance: 0.00,
        freeze: 0,
        score: 0,
        score_sign_continuous: 0,
        cashlogs: undefined,
        wallet: 0,
        frozen: 0,
        walletConsump: 0
    },
    goConsumption() {
        wx.navigateTo({
            url: './detail/detail',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    getwallet() {
        let that = this;
        wx.request({
            url: 'https://www.brvpmusic.com/api/customers/wallet',
            method: 'GET',
            data: {
                'openid': wx.getStorageSync('OpenId'),
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res.data);
                console.log('返回成功');
                var wallet = res.data.body.wallet;
                var frozen = res.data.body.frozen;
                var walletConsump = res.data.body.walletConsump;
                that.setData({
                    walletConsump: walletConsump,
                    frozen: frozen,
                    wallet: wallet
                })
            },
            fail: function(res) {
                console.log('返回失败');
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getwallet()


        const _this = this
        const token = wx.getStorageSync('token')
        if (!token) {
            app.goLoginPageTimeOut()
            return
        }
        WXAPI.userAmount(token).then(function(res) {
                if (res.code == 700) {
                    wx.showToast({
                        title: '当前账户存在异常',
                        icon: 'none'
                    })
                    return
                }
                if (res.code == 2000) {
                    app.goLoginPageTimeOut()
                    return
                }
                if (res.code == 0) {
                    _this.setData({
                        balance: res.data.balance.toFixed(2),
                        freeze: res.data.freeze.toFixed(2),
                        totleConsumed: res.data.totleConsumed.toFixed(2),
                        score: res.data.score
                    });
                }
            })
            // 读取资金明细
        WXAPI.cashLogs({
            token: token,
            page: 1,
            pageSize: 50
        }).then(res => {
            if (res.code == 0) {
                _this.setData({
                    cashlogs: res.data
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    recharge: function(e) {
        WXAPI.addTempleMsgFormid({
            token: wx.getStorageSync('token'),
            type: 'form',
            formId: e.detail.formId
        })
        wx.navigateTo({
            url: "/pages/recharge/index"
        })
    },
    withdraw: function(e) {
        WXAPI.addTempleMsgFormid({
            token: wx.getStorageSync('token'),
            type: 'form',
            formId: e.detail.formId
        })
        wx.navigateTo({
            url: "/pages/withdraw/index"
        })
    }
})